package com.linhngua.code.gwt.client.ui;

import java.util.Collection;
import java.util.Currency;
import java.util.Locale;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.linhngua.code.gwt.client.RemoteInvocation;
import com.linhngua.code.gwt.client.RemoteInvocationAsync;
import com.linhngua.code.gwt.client.data.shop.Item;
import com.linhngua.code.gwt.shared.FormFieldName;

public class ItemEdit extends Composite {

	private RemoteInvocationAsync remoteInvocationAsync = GWT
			.create(RemoteInvocation.class);

	private static ItemEditUiBinder uiBinder = GWT
			.create(ItemEditUiBinder.class);

	interface ItemEditUiBinder extends UiBinder<Widget, ItemEdit> {
	}

	@UiField
	FormPanel form;
	@UiField
	TextBox nameBox;
	@UiField
	TextBox priceBox;
	@UiField
	ListBox currencyList;
	@UiField
	CheckBox onSale;
	@UiField
	TextBox discountBox;
	@UiField
	FileUpload fileUpload1;
	@UiField
	FileUpload fileUpload2;
	@UiField
	FileUpload fileUpload3;
	@UiField
	Button submitButton;
	@UiField
	Button cancelButton;
	

	@UiField
	TextArea descriptionArea;

	public ItemEdit() {
		initWidget(uiBinder.createAndBindUi(this));
		form.setAction(GWT.getHostPageBaseURL() + "rs/item");
		form.setMethod(FormPanel.METHOD_POST);
		form.setEncoding(FormPanel.ENCODING_MULTIPART);
		nameBox.setName(FormFieldName.ITEM_NAME.name());
		priceBox.setName(FormFieldName.ITEM_PRICE.name());
		currencyList.setName(FormFieldName.ITEM_CURRENCY.name());
		onSale.setName(FormFieldName.ITEM_ONSALE.name());
		discountBox.setName(FormFieldName.ITEM_DISCOUNT.name());
		fileUpload1.setName(FormFieldName.ITEM_IMAGE1.name());
		fileUpload2.setName(FormFieldName.ITEM_IMAGE2.name());
		fileUpload3.setName(FormFieldName.ITEM_IMAGE3.name());
		descriptionArea.setName(FormFieldName.ITEM_DESCRIPTION.name());
		remoteInvocationAsync
				.listCurrencies(new AsyncCallback<Collection<String>>() {

					@Override
					public void onFailure(Throwable paramThrowable) {
						Window.prompt("Error", "Failed to get currency list");

					}

					@Override
					public void onSuccess(Collection<String> currencies) {
						for (String cur : currencies) {
							currencyList.addItem(cur);
						}

					}
				});
	}

	// validation

	@UiHandler({ "nameBox", "priceBox", "discountBox" })
	void boxChanged(ChangeEvent event) {
		nameBox.removeStyleName("error");
		priceBox.removeStyleName("error");
		discountBox.removeStyleName("error");
	}

	boolean validate(String name, String price, String discount,
			boolean isOnSale) {

		boolean ok = true;
		ok &= (name != null && name.trim().length() > 0);

		if (!ok) {
			nameBox.setStyleName("error");
			nameBox.setText("Name should not be empty");
			return ok;
		}

		try {
			double priceValue = Double.parseDouble(price);
			ok &= priceValue > 0;
			if (!ok) {
				priceBox.setText("Invalid price");
				priceBox.setStyleName("error");
				return ok;
			}

		} catch (NumberFormatException ex) {
			priceBox.setText("Invalid price");
			priceBox.setStyleName("error");
			return false;
		}

		if (isOnSale) {
			try {
				double discountValue = Double.parseDouble(discount);
				ok &= (discountValue > 0 & discountValue < 100);
				if (!ok) {
					discountBox
							.setText("Must greater than 0 and less than 100");
					discountBox.setStyleName("error");
					return ok;
				}
			} catch (NumberFormatException ex) {
				discountBox.setText("Not a number");
				discountBox.setStyleName("error");
				return false;
			}

		}
		return ok;
	}

	// /end validation

	@UiHandler("submitButton")
	void onSubmit(ClickEvent clickEvent) {
		String name = nameBox.getText();
		String price = priceBox.getText();
		boolean isOnSale = onSale.getValue();
		String discount = discountBox.getText();
		boolean valid = validate(name, price, discount, isOnSale);
		if (valid) {
			form.submit();
		}
	}

	@UiHandler("cancelButton")
	void onCancel(ClickEvent clickEvent) {
		form.reset();
	}

	@UiHandler("onSale")
	void onSaleClicked(ClickEvent clickEvent) {
		if (onSale.getValue()) {
			discountBox.setEnabled(true);
		} else {
			discountBox.setEnabled(false);
		}
	}
}
